﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using LoginServer.Converter;

namespace LoginServer
{
    public partial class LoginServer
    {
        public List<ManagerMachine> list_manager_machine;        
        public ManagerUserConnection manager_user;
        public GetDB AccessDB;
        public ConvertDataMachine convert_data;
        public ManagerRoom manager_room;
        CONFIG config;
        DistributedProcessing distributed;        
        string path_config = Directory.GetCurrentDirectory() + "\\Config\\CONFIG.xml";
        

        public LoginServer()
        {
            LoadConfig();            
            Init();
        }

        public void Init()
        {
            AccessDB = new GetDB();
            //AccessDB.RemoveAllTable();
            //AccessDB.RemoveAllRoom();
            manager_user = new ManagerUserConnection(config.IPSERVER, config.PORTSERVER, config, this);
            list_manager_machine = new List<ManagerMachine>();
            distributed = new DistributedProcessing();
            manager_room = new ManagerRoom(AccessDB);
            convert_data = new ConvertDataMachine();
            for (int i = 0; i < config.MACHINES.Count; i++)
                list_manager_machine.Add(new ManagerMachine(config, config.MACHINES[i].ID,this, manager_room));

        }

        void LoadConfig()
        {
            config = CONFIG.Load(path_config);
        }

        public void StartLoginServer()
        {
            manager_user.Start();
            StartAllMachine();
        }

        public void StartAllMachine()
        {
            for (int i = 0; i < list_manager_machine.Count;i++ )
            {
                list_manager_machine[i].Start();
            }
        }
      
    }
}
